package dsd;

import com.thoughtworks.xstream.XStream;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Simple..
 *
 * @author Lizhong
 * @date 2019/7/13
 */
public class Simple {
    @Test
    public void test() {
        method01();
        method02();

    }

    public void method01() {
        XStream xstream = new XStream();
        xstream.alias("person", People.class);
        People joe = new People();
        joe.setName("zhang三");
        joe.setAge(12);
        String xml = xstream.toXML(joe);
        System.out.println(xml);
    }

    public void method02() {
        People people = new People();
        people.setAge(12);
        People people0 = new People();
        people0.setAge(12);
        Response result = new Response();
        ;
        result.setPage(10);
        result.setResutl(new ArrayList<People>(Arrays.asList(people, people0)));
        XStream xstream = new XStream();
        String xml = xstream.toXML(result);
        System.out.println(xml);
    }

    public static void main(String[] args) {
        // testContextClassLoader();
        method01(Thread.currentThread().getContextClassLoader());
    }

    /*  public static void testContextClassLoader() {
          //获取extclassloader

          System.out.println(ClassLoader.getSystemClassLoader());
          ClassLoader classLoader = CustomeLoad.class.getClassLoader();
          ClassLoader extClassloader = classLoader.getParent();
          System.out.println("current:" + classLoader);
          System.out.println("extloader:"  +extClassloader);
          ClassLoader bootstrap = extClassloader.getParent();
          System.out.println("bootstrap:"  + bootstrap);
          if(bootstrap!=null) {
              System.out.println("bootstrap+parnt:" + bootstrap.getParent());
          } else {
              System.out.println("bootstrap+parnt: null");

          }
          System.out.println("before current thread context loader:"  +Thread.currentThread().getContextClassLoader());
          //设置当前线程上下文加载器为ext
          Thread.currentThread().setContextClassLoader(extClassloader);
          ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
          Iterator<Driver> iterator = loader.iterator();
          while (iterator.hasNext()) {
              Driver driver = (Driver) iterator.next();
              System.out.println("driver:" + driver.getClass() + ",loader:" + driver.getClass().getClassLoader());
          }
          System.out.println("after current thread context loader:"  +Thread.currentThread().getContextClassLoader());
          System.out.println("current loder:" + CustomeLoad.class.getClassLoader());
          System.out.println("ServiceLoader loader:" + ServiceLoader.class.getClassLoader());
      }*/
    public static void method01(ClassLoader contextClassLoader) {
        System.out.println(contextClassLoader);
        while (contextClassLoader != null) {
            contextClassLoader = contextClassLoader.getParent();
            System.out.println(contextClassLoader);
        }
    }
}

